package com.mobilesalon.activity;

import android.database.Cursor;
import android.util.Log;
import android.widget.Toast;

import com.mobilesalon.MobileSalonApplication;
import com.mobilesalon.MobileSalonFactory;
import com.mobilesalon.R;
import com.mobilesalon.business.ConfigurationException;
import com.mobilesalon.provider.ServiceClientContract.Appointment;
import com.mobilesalon.provider.ServiceClientContract.Service;
import com.mobilesalon.provider.ServiceClientContract.ServiceClient;
import com.mobilesalon.tools.DateTools;
import com.mobilesalon.tools.MyCursorAdapter;

public class DetailedMonthReport extends MonthReport implements Iconable {

	private static final String TAG = DetailedMonthReport.class.getName();

	protected int getContentView() {
		return R.layout.service_month_report_detailed;
	}

	protected void displayReport() {
		String month = DateTools.MONTH_DATEFORMAT.format(current.getTime());
		this.setReportTitle(String.format(getString(R.string.service_month_report_title), month));

		Log.d(TAG, "Display report for date:" + month);

		try {
			report = serviceClientReportManager.fetchDetailledMonthAppointments(this.current);
		} catch (ConfigurationException e) {
			Toast.makeText(MobileSalonFactory.getContext(), e.getErrorMessageRessourceId(),
					Toast.LENGTH_LONG).show();
			return;
		}

		float totalAmount = 0;
		Cursor cursor = report.getMonthReportCursor();
		startManagingCursor(cursor);

		cursor.moveToPosition(-1);
		while (cursor.moveToNext()) {
			totalAmount = totalAmount + cursor.getFloat(cursor.getColumnIndex(ServiceClient.PRICE));
		}

		totalAmountTextView.setText("" + report.getSumAmountService());

		// Create a mapping between database columns and layout fields
		String[] from = new String[] { Appointment.DATE, ServiceClient.CLIENT_DISPLAY_NAME,
				Service.SERVICE_NAME, ServiceClient.PRICE };
		int[] to = new int[] { R.id.service_month_report_detailed_date,
				R.id.service_month_report_detailed_client_name,
				R.id.service_month_report_detailed_service_name,
				R.id.service_month_report_detailed_price };

		// Create a cursor adapter and set it to display using a row layout
		MyCursorAdapter adapter = new MyCursorAdapter(this,
				R.layout.service_month_report_detailed_row, cursor, from, to);

		setListAdapter(adapter);
	}
	public int getIconResource() {
		return  R.drawable.icon;
	}

	public String getIconName() {
		return MobileSalonApplication.getAppContext().getString(R.string.detailed_service_month_report_icon);
	}

}
